// pages/detail/detail.js
let isdianzan=false
var app=getApp()
Page({
    // 长按保存功能---授权部分
    saveImage (e) {
        let _this = this
        wx.showActionSheet({
            itemList: ['保存到相册'],
            success(res) {
                let url = e.currentTarget.dataset.url;
                wx.getSetting({
                    success: (res) => {
                        if (!res.authSetting['scope.writePhotosAlbum']) {
                            wx.authorize({
                                scope: 'scope.writePhotosAlbum',
                                success: () => {
                                    // 同意授权
                                    _this.saveImgInner(url);
                                },
                                fail: (res) => {
                                    console.log(res);
                                    wx.showModal({
                                        title: '保存失败',
                                        content: '请开启访问手机相册权限',
                                        success(res) {
                                            wx.openSetting()
                                        }
                                    })
                                }
                            })
                        } else {
                            // 已经授权了
                            _this.saveImgInner(url);
                        }
                    },
                    fail: (res) => {
                        console.log(res);
                    }
                })   
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })
    },
    // 长按保存功能--保存部分
    saveImgInner (url) {
        wx.getImageInfo({
            src: url,
            success: (res) => {
                let path = res.path;
                wx.saveImageToPhotosAlbum({
                    filePath: path,
                    success: (res) => {
                        console.log(res);
                        wx.showToast({
                            title: '已保存到相册',
                        })
                    },
                    fail: (res) => {
                        console.log(res);
                    }
                })
            },
            fail: (res) => {
                console.log(res);
            }
        })
    },
    //点击预览图片   
    preview(){
        wx.previewImage({
            current:this.data.article.url,
            urls: [this.data.article.url],
        })
    },
    // 点击收藏
    addFavorites:function(options){
        let article=this.data.article;
        wx.setStorageSync(article._id,article);
        this.setData({isAdd:true});
    },
    // 点击取消收藏
    cancelFavorites:function(options){
        let article=this.data.article;
        wx.removeStorageSync(article._id);
        this.setData({isAdd:false});
    },
    /**
     * 页面的初始数据
     */
    data: {
        comment:[],
        pinlunlist:[],
        dianzan:"../../image/dianzan-black.png",
        dianzannum:0
    },
    // 点赞功能实现
    dianzan(){
        if (this.data.info.length==0) {
            wx.showToast({
              title: '先滚去登录啊',
              icon:'none'
            })
            return
        }else{
            if (isdianzan) {
                this.setData({
                    dianzan:"../../image/dianzan-black.png",
                    dianzannum:this.data.dianzannum-1
                })
                isdianzan=false
            }else{
                this.setData({
                    dianzan:'../../image/dianzan-red.png',
                    dianzannum:this.data.dianzannum+1,
                })
                isdianzan=true
            }
            wx.cloud.database().collection('commentlist').doc(this.data._id)
            .update({
                data:{
                    dianzannum:this.data.dianzannum
                }
            })
        }
     
       
       
    },
    // 获取输入的评论
    getcomment(e){
        this.setData({comment:e.detail.value})
    },
    // 上传用户评论信息
    pushcomment(){
        // console.log(this.data.comment)
        if (this.data.info.length==0) {
            wx.showModal({
                title:'提示',
                content:'请先登录哦',
                success(res){
                    wx.switchTab({
                      url: '/pages/my/my',
                    })
                }
            })
            return
        }
        if(this.data.comment.length==0){
            wx.showToast({
              title: '请输入内容',
              icon:'error'
            })
            return
        }
        else{
            let pinlunitem={}
            pinlunitem.infonickname=this.data.info.nickName
            pinlunitem.infoimage=this.data.info.avatarUrl
            pinlunitem.pinlun=this.data.comment
            this.data.pinlunlist.push(pinlunitem)
            console.log('发表评论后的列表',this.data.pinlunlist)
            
            wx.cloud.callFunction({
                name:"pinlun",
                data:{
                    id:this.data._id,
                    comment:this.data.pinlunlist
                }
            })
            .then(res=>{
                wx.showToast({
                  title: '已评论',
                  icon:'none'
                })
                this.setData({pinlunlist:this.data.pinlunlist})
            })
            .catch(res=>{
                wx.showToast({
                  title: '哪里出错了',
                  icon:'none'
                })
            })
            }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({info:app.globalData.info})
        console.log(this.data.info.nickName)
        console.log(this.data.info.avatarUrl)
        let id =options.id;
        this.setData({_id:id})
        // 查询页面详细内容
        wx.cloud.database().collection('commentlist').doc(id).get()
        .then(res=>{
            console.log('doc查询',res.data)
            this.setData({
                article:res.data,
                pinlunlist:res.data.comment,
                dianzannum:res.data.dianzannum
            })
            console.log(this.data.pinlunlist)
        })
        var article=wx.getStorageSync(id);
        if(article!=''){
            this.setData({isAdd:true})
        }
        else{
            this.setData({isAdd:false})
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})